function [nmodel]=i_woa05(dat,model)

% -----------------------------------------------------------
% -- Interpolating modelled fields on observation's position
% -- in the "climatology" WOA05 case
% -----------------------------------------------------------
% G. Charria (10/2008)


% ---- Create the Structure ...
nmodel = struct('dummy',1);
    

% ---- Control that the model is covering the same period as the data

% ---- Interpolation    
d1(1:size(model.data,1),1:size(model.data,2),1:size(model.data,3),1:size(dat.data,4))=NaN;       
d2(1:size(model.data,1),1:size(dat.data,2),1:size(dat.data,3),1:size(dat.data,4))=NaN;
h=waitbar(0,'Horizontal and temporal interpolation ...');
for iz=1:size(model.data,1)

    % ---- I. Interpolation over the cruise data (time) ----

    d1(iz,:,:,:)=monthyl_nemo_mean(squeeze(model.data(iz,:,:,:)),model.time);

    %figure
    %V=datevec(model.time);
    %plot(V(:,2),squeeze(model.data(1,150,150,:)),'.')
    %hold on
    %plot(squeeze(d1(1,150,150,:)),'r+')
    %V=[];
    %clear V

    % ---- II. Interpolation over the cruise track (lon,lat) ----

    for it=1:size(dat.data,4)
        waitbar(   ((iz-1)*(size(dat.data,4)) + it)/((size(dat.data,4))*size(model.data,1)),h) 
        d2(iz,:,:,it)=griddata(model.lon,model.lat,squeeze(d1(iz,:,:,it)),dat.londata',dat.latdata)';
        % ---
        %figure
        %subplot(1,2,1)
        %pcolor(model.lon,model.lat,squeeze(model.data(iz,:,:,it))); shading flat; colorbar;
        %subplot(1,2,2)
        %pcolor(dat.londata,dat.latdata,squeeze(d2(iz,:,:,it))')
        %shading flat
        %pause            
    end     


end
close(h)        

% ---- III. Interpolation over the cruise depths ----   
d2(isnan(d2))=0.;
h=waitbar(0,'Vertical interpolation ...');    
d3(1:size(dat.data,1),1:size(dat.data,2),1:size(dat.data,3),1:size(dat.data,4))=NaN;
for it=1:size(dat.data,4)
    for ila=1:size(dat.data,3)
        for ilo=1:size(dat.data,2)
            lt=size(dat.data,4)*size(dat.data,2)*size(dat.data,3);
            waitbar( ( (it-1)*size(dat.data,3) + (ila-1)*size(dat.data,2) + ila ) /lt   ,h) 

            d3(:,ilo,ila,it)=interp1(model.depth,squeeze(d2(:,ilo,ila,it)),dat.depthdata);
            %if (ilo==150 && ila==150)
            % ---
            %figure
            %plot(dat.depthdata,squeeze(d3(:,ilo,ila,it)),'+')
            %hold on
            %plot(model.depth,squeeze(d2(:,ilo,ila,it)),'or')
            %pause
            %end

        end
    end
end

close(h)



nmodel.lon=dat.londata;
nmodel.lat=dat.latdata;
nmodel.data=d3;
nmodel.depth=dat.depthdata;
nmodel.time=dat.timedata;


% ---- Clean the Structure ...
nmodel=rmfield(nmodel,'dummy');    
